<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
<!--        <el-form-item label="关键词：">-->
<!--          <el-input v-model="listPram.keyword" placeholder="请输入关键词" clearable class="selWidth"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item>-->
<!--          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>-->
<!--          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--        </el-form-item>-->
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)"
                     v-hasPermi="['admin:indicators:create']">添加指标
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="地区" prop="area_name" min-width="80"/>
        <el-table-column label="人才总量(万人)" prop="total" min-width="120"/>
        <el-table-column label="人才占比(%)" prop="talent_proportion" min-width="120"/>
        <el-table-column label="人才发展专项资金(万元)" prop="money" min-width="120"/>
        <el-table-column label="人才总量(增长率)" prop="total_rate" min-width="120"/>
        <el-table-column label="人才发展专项资金(增长率)" prop="money_rate" min-width="120"/>
        <el-table-column label="状态" min-width="100">
          <template slot-scope="scope">
            <el-switch
              :disabled="!checkPermi(['admin:indicators:status'])"
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="130" fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:indicators:update']">编辑
            </el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row.id)" v-hasPermi="['admin:indicators:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '添加指标':'编辑指标'"
      destroy-on-close
      top="10vh"
      :close-on-click-modal="false"
      width="800px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :area="area"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import {checkPermi} from "@/utils/permission";
import {getCategory} from '@/api/categoryApi';
import {deleteIndicators, getIndicatorsList, updateIndicatorsStatus} from "@/api/indicators";
export default {
  components: {edit},
  data() {
    return {
      constants: this.$constants,
      loading: false,
      listData: {list: []},
      listPram: {
        keyword: '',
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      area:[],
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      }
    }
  },
  mounted() {
    this.getTableList()
    this.getAreaList();
  },
  methods: {
    getAreaList(){
      getCategory({name:'区域'}).then(data=>{
        this.area=data;
      })
    },
    checkPermi,
    onchangeIsShow(row) {
      updateIndicatorsStatus({id: row.id, status: row.status}).then(async () => {
          this.$message.success('修改成功');
        }).finally(() => {
        this.getTableList()
      })
    },
    handleSearch() {
      this.listPram.page = 1
      this.getTableList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getTableList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getTableList()
    },
    handlerOpenDel(id) {
      this.$confirm('确认删除当前数据').then(() => {
        deleteIndicators({id}).then(data => {
          this.$message.success('删除数据成功')
          this.getTableList()
        })
      })
    },
    getTableList() {
      this.loading=true;
      getIndicatorsList(this.listPram).then(data => {
        this.listData = data
      }).finally(()=>{
        this.loading=false;
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.getTableList()
    },
    resetQuery() {
      this.listPram.keyword = '';
      this.getTableList()
    }
  }
}
</script>

<style scoped>

</style>
